package com.iqiyi.dbagent.base.log;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;

/**
 * @Title: XLogger.java
 * @Package com.schoolkeeper.log
 * @Description: 日志记录工具
 * @author 王年明
 * @date 2015年1月1日 下午4:38:40
 * @version V1.0
 */
public class XLogger {
    private static LoggerContext logContext = null;

    private static Logger logger = LoggerFactory.getLogger(XLogger.class);

    private static Logger MainLogger = null;

    private static Logger ErrLogger = null;

    private static Logger TestLogger = null;
    
    private PrintWriter logPrint;
    private String logFile = "";
    private String logName = "";

    /**
     * 配置没有头的log
    */
    public XLogger(){
        checkDate();
    }

    /**
     * 配置log文件名的头
    */
    public XLogger(String logName){
        this.logName = logName;
        checkDate();
    }
    
    static{
        XLogger.init();
    }
    public static void init() {
        if (logContext == null) {
            logContext = (LoggerContext) LoggerFactory.getILoggerFactory();
            JoranConfigurator configurator = new JoranConfigurator();
            configurator.setContext(logContext);
            logger.info("初始化日志...");
            // logContext.reset();
            try {
                configurator.doConfigure("logconfig.xml");
            } catch (JoranException e) {
                e.printStackTrace();
            }
            // StatusPrinter.printInCaseOfErrorsOrWarnings(logContext);
        }
        if (null == MainLogger) {
            MainLogger = LoggerFactory.getLogger("mainlog");
        }
        if (null == ErrLogger) {
            ErrLogger = LoggerFactory.getLogger("errlog");
        }
        if (null == TestLogger) {
            TestLogger = LoggerFactory.getLogger("testlog");
        }
    }

    public Logger getMainLogger() {
        return MainLogger;
    }

    public void setMainLogger(Logger mainLogger) {
        MainLogger = mainLogger;
    }

    public Logger getErrLogger() {
        return ErrLogger;
    }

    public void setErrLogger(Logger errLogger) {
        ErrLogger = errLogger;
    }

    public static Logger getTestLogger() {
        return TestLogger;
    }

    public static void setTestLogger(Logger testLogger) {
        TestLogger = testLogger;
    }

    /**
     * 得到log文件名
    */
    private String getLogFile(){
        String date = "";
        Calendar cd = Calendar.getInstance();
        int y = cd.get(Calendar.YEAR);
        int m = cd.get(Calendar.MONTH) + 1;
        int d = cd.get(Calendar.DAY_OF_MONTH);

        date = "./log/" + logName + y + "-";

        if(m < 10)
            date += 0;

        date += m + "-";

        if(d < 10)
            date += 0;

        date += d + ".log";

        return date;
    }

    /**
     * 配置log属性，如果没有新建log文件
    */
    private void newLog(){
        logFile = getLogFile();

        try{
            logPrint = new PrintWriter(new FileWriter(logFile, true), true);
        }catch(IOException e){
            (new File("./log")).mkdir();

            try{
                logPrint = new PrintWriter(new FileWriter(logFile, true), true);

 }catch(IOException ex){
                System.err.println("无法打开日志文件：" + logFile);
                logPrint = new PrintWriter(System.err);
            }
        }
    }

    /**
     * 检查日期如果改变新建log文件
    */
    private void checkDate(){
        if(logFile == null || logFile.trim().equals("") || !logFile.equals(getLogFile())){
            newLog();
        }
    }

    /**
     * 将文本信息写入日志文件，缺省为userdir/log
    */
    public void log(String msg) {
        checkDate();
        logPrint.println(new Date() + ": " + msg);
    }

    /**
     * 将文本信息与异常写入日志文件
    */
    public void log(Throwable e, String msg) {
        checkDate();
        logPrint.println(new Date() + ": " + msg);
        e.printStackTrace(logPrint);
    }
}
